package com.xspace.android.xafcommon.util;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;

/**
 * 配置文件读取工具类
 * <p/>
 * create time 2013-12-16 version 1.0.0.0 class PreferencesUtils.java
 *
 * @author mawen
 */
public class SharedPreferencesUtils {
    private static final String NAME = "preferences.properties";

    public static synchronized void setPfValue(Context context, String name, Object value, String clsName) {
        // try {
        // Properties p = new Properties();
        // p.load(context.getAssets().open(NAME));
        // p.put(name, value);
        // } catch (Exception e) {
        // return;
        // }
        SharedPreferences mCfgPrf = context.getSharedPreferences(NAME, MODE_MULTI_PROCESS);
        LogUtils.d("key name》》" + name + "key value >>>" + value);
        if (clsName.equals(Boolean.class.getName())) {
            mCfgPrf.edit().putBoolean(name, (Boolean) value).commit();
        } else if (clsName.equals(Integer.class.getName())) {
            mCfgPrf.edit().putInt(name, (Integer) value).commit();
        } else if (clsName.equals(Long.class.getName())) {
            mCfgPrf.edit().putLong(name, (Long) value).commit();
        } else if (clsName.equals(Float.class.getName())) {
            mCfgPrf.edit().putFloat(name, (Float) value).commit();
        } else {
            mCfgPrf.edit().putString(name, (String) value).commit();
        }
    }

    public static synchronized Object getPfValue(Context context, String name, String clsName) {
        // Object value = null;
        // try {
        // Properties p = new Properties();
        // p.load(context.getAssets().open(NAME));
        // value = p.get(name);
        // } catch (Exception e) {
        // if (clsName.equals(Boolean.class.getName())) {
        // value = false;
        // } else if (clsName.equals(Integer.class.getName())) {
        // value = -1;
        // } else if (clsName.equals(Long.class.getName())) {
        // value = -1L;
        // } else if (clsName.equals(Float.class.getName())) {
        // value = -1f;
        // } else {
        // value = "";
        // }
        // }
        SharedPreferences mCfgPrf = context.getSharedPreferences(NAME, MODE_MULTI_PROCESS);
        Object value = null;
        if (clsName.equals(Boolean.class.getName())) {
            value = mCfgPrf.getBoolean(name, false);
        } else if (clsName.equals(Integer.class.getName())) {
            value = mCfgPrf.getInt(name, -1);
        } else if (clsName.equals(Long.class.getName())) {
            value = mCfgPrf.getLong(name, -1L);
        } else if (clsName.equals(Float.class.getName())) {
            value = mCfgPrf.getFloat(name, -1f);
        } else {
            value = mCfgPrf.getString(name, "");
        }
        return value;
    }

    public static synchronized void handlerOnSharedPreferenceChangeListener(Context context,
                                                                            OnSharedPreferenceChangeListener listener, boolean isReg) {
        SharedPreferences mCfgPrf = context.getSharedPreferences(NAME, MODE_MULTI_PROCESS);
        if (isReg) {
            mCfgPrf.registerOnSharedPreferenceChangeListener(listener);
        } else {
            mCfgPrf.unregisterOnSharedPreferenceChangeListener(listener);
        }

    }

    private static final int MODE_MULTI_PROCESS = 0x0004;
}
